package com.manager.plat.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.manager.plat.dao.IListBlackDao;
import com.manager.plat.util.BaseUtil;
import com.manager.plat.vo.ListBlackVo;
/**
 * 黑名单数据实现
 * @author liugeng
 *
 */
public class ListBlackDaoImpl extends JdbcDaoSupport implements IListBlackDao {
	private static Logger logger = Logger.getLogger(ListBlackDaoImpl.class);
	/**获得白名单列表*/
	@SuppressWarnings("unchecked")
	@Override
	public List<ListBlackVo> getListBlackList() {
		String sql = "select id, mobile, sp_code from tb_ivr_list_black where status = 1";
		logger.info(String.format("获得黑名单列表[%s]", sql));
		
		List<ListBlackVo> listBlackVoList = this.getJdbcTemplate().query(sql, rowMapper);
		if(listBlackVoList != null && listBlackVoList.size() > 0){
			return listBlackVoList;
		}
		return null;
	}
	/**根据ID获得白名单信息*/
	@SuppressWarnings("unchecked")
	@Override
	public ListBlackVo getListBlackVoById(int id) {
		String sql = "select id, mobile, sp_code from tb_ivr_list_black where id = ? and status = 1";
		Object[] param = new Object[]{id};
		logger.info(String.format("根据ID获得黑名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		List<ListBlackVo> listBlackVoList = this.getJdbcTemplate().query(sql, param, rowMapper);
		if(listBlackVoList != null && listBlackVoList.size() > 0){
			return listBlackVoList.get(0);
		}
		return null;
	}
	
	private ParameterizedRowMapper<ListBlackVo> rowMapper = new ParameterizedRowMapper<ListBlackVo>() {

		@Override
		public ListBlackVo mapRow(ResultSet rs, int rowNum) throws SQLException {
			ListBlackVo listBlackVo = new ListBlackVo();
			listBlackVo.setId(rs.getInt("id"));
			listBlackVo.setMobile(rs.getString("mobile"));
			listBlackVo.setSp_code(rs.getString("sp_code"));
			return listBlackVo;
		}
	};
	/**新增白名单信息*/
	@Override
	public int batchInsertListBlack(List<ListBlackVo> listBlackList) {
		String sql = "insert into tb_ivr_list_black (mobile, sp_code, status) values(?,?,1)";
		
		int[] count = this.getJdbcTemplate().batchUpdate(sql, new autoInsertListBlackPsSetter(listBlackList));
		if(count.length > 0){
			return count.length;
		}
		return 0;
	}
	
	private class autoInsertListBlackPsSetter implements BatchPreparedStatementSetter{
		final List<ListBlackVo> listBlackListTemp;
		public autoInsertListBlackPsSetter(List<ListBlackVo> listBlackList){
			listBlackListTemp = listBlackList;
		}
		
		@Override
		public void setValues(PreparedStatement ps, int i) throws SQLException {
			ListBlackVo listBlackVo = listBlackListTemp.get(i);
			ps.setString(1, listBlackVo.getMobile());
			ps.setString(2, listBlackVo.getSp_code());
		}
		@Override
		public int getBatchSize() {
			return listBlackListTemp.size();
		}
	}
	
	
	/**修改白名单信息*/
	@Override
	public int updateListBlack(ListBlackVo listBlackVo) {
		String sql = "update tb_ivr_list_black set mobile = ?, sp_code = ? where id = ?";
		Object[] param = new Object[]{listBlackVo.getMobile(), listBlackVo.getSp_code(), listBlackVo.getId()};
		logger.info(String.format("修改黑名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
	/**删除白名单信息*/
	@Override
	public int deleteListBlack(int id) {
		String sql = "update tb_ivr_list_black set status = 0 where id = ?";
		Object[] param = new Object[]{id};
		logger.info(String.format("删除黑名单信息[%s]", BaseUtil.logSQL(sql, param)));
		
		int count = this.getJdbcTemplate().update(sql, param);
		return count;
	}
}
